// index.js
// 获取应用实例
const app = getApp();
var topOpacity = 0;
Page({
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: false, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    pictures: [app.baseUrl + 'images/index/banner.png?v=' + app.version, app.baseUrl + 'images/index/banner.png?v=' + app.version, app.baseUrl + 'images/index/banner.png?v=' + app.version, app.baseUrl + 'images/index/banner.png?v=' + app.version],
    swiperNum: 0,

    openid: '',
    shareTitle: '',
    shareImg: '',
    bannerList: [],
    isStatus: 0,
    showBirthdayDom: false,
    yzName: '',
    isOne: 1,
    hideView: false,
    isPlayed: false,
    muted: false,
    is_hide_shop: 0,
    showParticipate:false,
    indexAdInfo:[],
    showMaintenance:false,// 提示弹窗
  },
  maintenance() {
    this.setData({
      showMaintenance: false
    })
  },
  popclose() {
    this.setData({
      showMaintenance: false
    })
  },
  goSearch(){
    wx.navigateTo({
      url: '/pages/search/index',
    })
  },
  audioPlayed() {
    wx.setStorageSync('playHomeVideo', true);
    var timeout1 = setTimeout(() => {
      app.show(this, 'hide_video_animate', 0);
      var timeout = setTimeout(() => {
        this.setData({
          hideView: true
        });
        clearTimeout(timeout)
      }, 800);
      clearTimeout(timeout1)
    }, 9000);



  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  swiperFn(e) {
    this.setData({
      swiperNum: e.detail.current,
    });
  },
  goShop() {
    wx.navigateTo({
      url: '/pages/shoppingMall/index',
    })
  },
  goMarketing() {
    wx.navigateTo({
      url: '/pages/marketing/index',
    })
  },
  rpx2px(rpx) {
    return rate * rpx
  },
  onLoad() {
    const {
      windowWidth
    } = wx.getSystemInfoSync()
    const rate = windowWidth / 750
    this.setData({
      fixedtop: 108 * rate
    })
    var that = this;
    let isPlayed = wx.getStorageSync('playHomeVideo');
    if (isPlayed) {
      this.setData({
        isPlayed: true,
        hideView: true,
      })
    } else {
      this.setData({
        isPlayed: false
      })
    }


    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getIndexPageInfo(res.data.openid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getIndexPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取登录code
  getIndexPageInfo(openid) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          that.getIndexPageData(openid, res.code);
        } else {
          that.getIndexPageData(openid, '');
        }
      },
      fail: function () {
        that.getIndexPageData(openid, '');
      }
    });
  },

  //获取本页数据
  getIndexPageData(openid, code) {
    var that = this;
    //获取发放权益的缓存
    var is_send_welfare = 0;
    var sendTimestamp = wx.getStorageSync('sendTimestamp');
    if (sendTimestamp == '' || sendTimestamp == null || sendTimestamp == undefined) {
      var timestamp = new Date().getTime();
      //缓存权益发放标识
      wx.setStorageSync('sendTimestamp', timestamp);
      is_send_welfare = 1;
    } else {
      //存在发放标识
      var timestamp = new Date().getTime();
      if (timestamp - sendTimestamp > 15000) {
        //缓存已超过10s
        is_send_welfare = 1;
        //重置发放时间戳
        wx.setStorageSync('sendTimestamp', timestamp);
      }
    }

    //发起网络请求
    wx.request({
      url: that.data.baseUrl + 'getIndexPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'code': code,
        'is_send_welfare': is_send_welfare
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          //$isStatus=0 不显示
          //$isStatus=1 显示 但未抽奖
          //$isStatus=2 显示 已抽奖
          //$isStatus=3 不显示 过期
          that.setData({
            bannerList: res.data.bannerList,
            isStatus: res.data.isStatus,
            yzName: res.data.yzName,
            isOne: 0,
            is_hide_shop: res.data.is_hide_shop,
            indexAdInfo:res.data.indexAdInfo
          });

          //判断是否显示生日
          if (res.data.isStatus == 1) {
            //显示生日抽奖
            that.setData({
              showBirthdayDom: true
            });
          }

          //判断是否显示广告
          if (res.data.is_show_index_ad == 1) {
            //显示广告
            that.setData({
              showParticipate: true
            });
          }
        } else {
          app.showTip(res.data.msg);
        }
      },
      fail: function () {
        app.showTip('抱歉,服务器繁忙');
      }
    });
  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  //跳转生日
  goBirth() {
    wx.navigateTo({
      url: '/pages/my/birthday'
    });
  },

  //关闭生日弹窗
  closeBirthdayDom() {
    this.setData({
      showBirthdayDom: false
    });
  },

  //点击banner图跳转
  headerDetail(e) {
    var type = e.currentTarget.dataset.type;
    var id = e.currentTarget.dataset.id;
    var activity = e.currentTarget.dataset.activity;
    var url = e.currentTarget.dataset.url;
    var mid = e.currentTarget.dataset.mid;

    if (type == 1) {
      //跳转值咨讯
      //判断是跳转微信稿还是详情
      if (url) {
        wx.navigateTo({
          url: '/pages/webView/webView?wx_url=' + url
        });
      } else {
        wx.navigateTo({
          url: '/pages/news/detail?id=' + id
        });
      }
    } else if (type == 2) {
      //跳转值活动
      if (activity == 1) {
        //跳转问卷
        wx.navigateTo({
          url: '/pages/activityCenter/detail?type=1&id=' + id
        });
      } else {
        //跳转活动详情
        wx.navigateTo({
          url: '/pages/activityCenter/detail?type=1&id=' + id
        });
      }
    } else if (type == 3) {
      //跳转值权益
      wx.navigateTo({
        url: '/pages/equity/detail?id=' + id + "&mid=" + mid
      });
    } else {
      //跳转秒杀或抽奖
      wx.navigateTo({
        url: '/pages/lotteryDrawPage/detail/index?id=' + id
      });
    }
  },

  //跳转各个板块的主页
  goNextDetail(e) {
    var that = this;
    var flag = e.currentTarget.dataset.flag;
    console.log(flag)
    if (flag === 'equity') {
      // 权益
      wx.navigateTo({
        url: '/pages/equity/index',
      })
    } else if (flag == 'life') {
      //跳转生活服务
      wx.navigateTo({
        url: '/pages/lifeService/index'
      });
    } else if (flag == 'news') {
      //跳转新闻
      wx.navigateTo({
        url: '/pages/news/index'
      });
    } else if (flag == 'activity') {
      //跳转活动
      wx.navigateTo({
        url: '/pages/activityCenter/index'
      });
    } else if (flag == 'order') {
      //跳转预约中心
      wx.navigateTo({
        url: '/pages/orderCenter/index'
      });
    } else if (flag == 'business') {
      //跳转麓商家
      wx.navigateTo({
        url: '/pages/business/index'
      });
    } else {
      //跳转麓社群
      wx.navigateTo({
        url: '/pages/association/index'
      });
    }
  },
  goBigCity() {
    wx.navigateTo({
      url: '/pages/bigCity/index'
    });
  },
  goQuestionnaire() {
    wx.navigateTo({
      url: '/pages/lifeService/question/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0,
      })
    }

    //判断是否领取过生日礼物了
    if (that.data.showBirthdayDom && that.data.isOne == 0 && that.data.isStatus == 1) {
      //刷新是否领取过生日礼物了
      wx.request({
        url: that.data.baseUrl + 'checkGetBirthPrize.php',
        method: 'POST',
        data: {
          'openid': that.data.openid
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == 1) {
            that.setData({
              showBirthdayDom: false
            });
          }
        },
        fail: function (res) {}
      });
    }
  },
  //关闭广告弹窗
  closeParticipate(){
    this.setData({
      showParticipate:false
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  },
  // 分享朋友圈
  onShareTimeline() {
    var that = this;
    return {
      title: that.data.shareTitle,
      query: 'is_share=1',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})